﻿using RightControl.Common;
using RightControl.IRepository;
using RightControl.IService;
using RightControl.Model;
using System.Collections.Generic;
using RightControl.Service;
using RightControl.Repository.Product;
using RightControl.Repository;

namespace RightControl.Service {
    public class SubProductSizeService : BaseService<SubProductSize>, ISubProductSizeService {
        public ISubProductSizeRepository repository { get; set; }

        public int SaveSubProductSize(IEnumerable<SubProductSize> entitys) {
            return repository.SaveSubProductSize(entitys);
        }


        public dynamic GetListByFilter(SubProductSize filter, PageInfo pageInfo) {

            //using (var conn = MySqlHelper.GetConnection()) {
            //    var sql = "select u.*,r.RoleName from t_User u INNER JOIN t_role r on u.RoleId=r.Id AND u.Id=@Id";
            //    return conn.Query<SubProductSize>(sql).FirstOrDefault();
            //}

            string _where = " where 1=1";
            _where += string.Format(" and ProductId = {0}", filter.ProductId);
            return GetListByFilter(filter, pageInfo, _where);
        }

        public bool UpdateSubProductSizes(IEnumerable<SubProductSize> entitys, string updatefields = null) {
            //"Size,Price,KeyAttributes"
            return repository.UpdateSubProductSizes(entitys, updatefields) > 0;
        }

        //public List<SubProduct> GetSubProductList(SubProduct filter, PageInfo pageInfo) {
        //    List<SubProduct> subProdList = new List<SubProduct>();
       
        //    dynamic subProdRet = GetListByFilter(filter, pageInfo);
        //    if (subProdRet != null && subProdRet.count > 0) {
        //        subProdList = subProdRet.data as List<SubProduct>;
        //    }
        //    return subProdList;
        //}

        public bool DeleteByProdcutId(int productId, int userId = 0) {
            string _where = " where 1=1";
            _where += string.Format(" and ProductId = {0}", productId);
            if (userId > 0) {
                _where += string.Format(" and CreateBy = {0}", userId);
            }
            return DeleteByWhere(_where);
        }

        public List<SubProductSize> GetSubProductSizeList(SubProductSize filter, PageInfo pageInfo) {
            List<SubProductSize> subProdList = new List<SubProductSize>();

            dynamic subProdRet = GetListByFilter(filter, pageInfo);

            if (subProdRet != null && subProdRet.count > 0) {
                subProdList = subProdRet.data as List<SubProductSize>;
            }
            return subProdList;
        }

        //public dynamic GetListByFilter(SubProductSize filter, PageInfo pageInfo) {
        //    return repository.GetByWhere(entitys);
        //}


    }
}